﻿using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Application.ManageConfigtable;
using wt.wine.Domain.Entities;

namespace wt.wine.Infrastructure.Persistence.Repository
{
    public class ConfigtableRepository : IConfigtableRepository
    {
        private readonly IApplicationDbContext _dbcontext;
        public ConfigtableRepository(IApplicationDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }


        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<PaginatedList<GetconfigtableDTO>> GetconfigtableList(int userid, int pageIndex, int pageSize, CancellationToken cancellationtoken)
        {
            var query = (from c in _dbcontext.configtable.AsNoTracking()
                         where c.binduserid == userid && c.state >= 0
                         orderby c.updatetime descending
                         select new GetconfigtableDTO()
                         {
                             id = c.id,
                             type = (c.type ?? 0),
                             capacitythreshold = c.capacitythreshold
                         });
            return await PaginatedList<GetconfigtableDTO>.CreateAsync(query, pageIndex, pageSize, cancellationtoken);
        }

        /// <summary>
        /// 修改前的查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userid"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<configtable> GetConfigTableById(int id, int userid, CancellationToken cancellationtoken)
        {
            return await _dbcontext.configtable.AsNoTracking().Where(p => p.id == id && p.binduserid == userid).FirstOrDefaultAsync(cancellationtoken);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task AddConfigTable(configtable model, CancellationToken cancellationtoken)
        {
            await _dbcontext.configtable.AddAsync(model, cancellationtoken);
            await _dbcontext.SaveChangesAsync(cancellationtoken);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task EditConfigTable(configtable model, CancellationToken cancellationtoken)
        {
            _dbcontext.Entry<configtable>(model).Property("capacitythreshold").IsModified = true;
            _dbcontext.Entry<configtable>(model).Property("updatetime").IsModified = true;
            _dbcontext.Entry<configtable>(model).Property("userid").IsModified = true;
            await _dbcontext.SaveChangesAsync(cancellationtoken);
        }

        /// <summary>
        /// 判断当前类型的配置是否存在
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="type"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<int> GetTypeCountByUserId(int userid,int type, CancellationToken cancellationtoken)
        {
            return await _dbcontext.configtable.AsNoTracking().Where(p => p.binduserid == userid && p.type == type && p.state >= 0).CountAsync(cancellationtoken);
        }
    }
}
